home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / extras / programm / gemfsc20 / gemfsc20.lzh / GEMFUNCS / RCINTERS.C < prev    next >
C/C++ Source or Header  |  1993-03-21  |  5KB  |  127 lines

  1. /**************************************************************************
  2.  * RCINTERS.C - Calc intersection of two GRECT rectangles.
  3.  *              Returns TRUE if rectanlges have common area, FALSE if not.
  4.  *************************************************************************/
  5.  
  6. #include "gemfintl.h"
  7.  
  8. #ifndef __HSC__ /* for non-hsc compilers, use portable C code */
  9.  
  10. short rc_intersect(prect1, prect2)
  11.     register GRECT *prect1;
  12.     register GRECT *prect2;
  13. {
  14.     register short    w1, w2;
  15.     short             lx, rx;
  16.     short             ty, by;
  17.  
  18.     /* calc right-side x as the lesser x of the two rectangles */
  19.  
  20.     w1 = prect1->g_x + prect1->g_w;
  21.     w2 = prect2->g_x + prect2->g_w;
  22.     rx  = (w1 < w2) ? w1 : w2;
  23.  
  24.     /*  calc bottom y as the lesser y of the two rectanlges */
  25.  
  26.     w1 = prect1->g_y + prect1->g_h;
  27.     w2 = prect2->g_y + prect2->g_h;
  28.     by  = (w1 < w2) ? w1 : w2;
  29.  
  30.     /* calc left-side x as the greater x of the two rectangles */
  31.  
  32.     w1 = prect1->g_x;
  33.     w2 = prect2->g_x;
  34.     lx = (w1 > w2) ? w1 : w2;
  35.  
  36.     /* calc top y as the greater y of the two rectangles */
  37.  
  38.     w1 = prect1->g_y;
  39.     w2 = prect2->g_y;
  40.     ty = (w1 > w2) ? w1 : w2;
  41.  
  42.     /* store the calculated rectangle (converting back to GRECT-type w/h) */
  43.  
  44.     prect2->g_x = lx;
  45.     prect2->g_y = ty;
  46.     prect2->g_w = rx - lx;
  47.     prect2->g_h = by - ty;
  48.  
  49.     /*
  50.      * if the calculated width or height is zero or less, it indicates
  51.      * that there is no overlap in at least one dimension, and thus no
  52.      * overlap in the rectangles, so return FALSE.  if both values are
  53.      * positive, there is a common intersecting area, so return TRUE.
  54.      */
  55.  
  56.     if ( prect2->g_w <= 0 || prect2->g_h <= 0) {
  57.         return 0;
  58.     } else {
  59.         return 1;
  60.    }
  61. }
  62.  
  63. #else   /* for HSC, use inline asm for smaller faster code */
  64. /*
  65. #pragma asm
  66.  
  67. ;*************************************************************************
  68. ;* RCINTERS.S - Calc intersection of two GRECT rectangles.
  69. ;*************************************************************************
  70.  
  71.           .globl     _rc_intersect
  72. _rc_intersect:
  73.  
  74.           move.l    4(sp),a1 
  75.           move.l    8(sp),a0 
  76.           movem.l   d3-d4,-(sp)
  77.                                         ; Calc right-side x...
  78.           move.w    (a1),d0             ;  rx1 = x1 + w1 
  79.           add.w     4(a1),d0            
  80.           move.w    (a0),d1             ;  rx2 = x2 + w2
  81.           add.w     4(a0),d1            
  82.           cmp.w     d0,d1               ;  compare rx1 <-> rx2
  83.           blt.s     .gotrx              ;  proper rx is the smaller
  84.           move.w    d0,d1               ;  of the two.
  85. .gotrx:   
  86.                                         ; Calc bottom y...
  87.           move.w    2(a1),d0            ;  by1 = y1 + h1
  88.           add.w     6(a1),d0            
  89.           move.w    2(a0),d2            ;  by2 = y2 + h2
  90.           add.w     6(a0),d2            
  91.           cmp.w     d0,d2               ;  compare by1 <-> by2
  92.           blt.s     .gotby              ;  proper by is the smaller
  93.           move.w    d0,d2               ;  of the two.       
  94. .gotby:                                 
  95.                                         ; Calc left-side x...
  96.           move.w    (a0),d3             ;  assume x2
  97.           cmp.w     (a1),d3             ;  compare x1 <-> x2  
  98.           bge.s     .gotlx              ;  proper lx is larger
  99.           move.w    (a1),d3             ;  of the two.
  100. .gotlx:                                 
  101.                                         ; Calc top y...
  102.           move.w    2(a0),d4            ;  assume y2
  103.           cmp.w     2(a1),d4            ;  compare y1 <-> y2 
  104.           bge.s     .gotty              ;  proper ty is larger
  105.           move.w    2(a1),d4            ;  of the two.
  106. .gotty:
  107.                                         ; Got all the x/y's...
  108.           moveq.l   #0,d0               ; Assume intersection is false.             
  109.           move.w    d3,(a0)+            ; store left x
  110.           move.w    d4,(a0)+            ; store top y
  111.           sub.w     d3,d1               ; compute width
  112.           ble.s     .done               ; if negative or zero, no intersect
  113.           move.w    d1,(a0)+            ; else store it
  114.           sub.w     d4,d2               ; compute height
  115.           ble.s     .done               ; if negative or zero, no intersect
  116.           move.w    d2,(a0)+            ; else store it
  117.           moveq.l   #1,d0               ; intersection is true 
  118. .done:
  119.           movem.l   (sp)+,d3-d4
  120.           tst.w     d0                  ; insure CCR return matches d0.
  121.           rts
  122.           
  123. #pragma endasm
  124. */
  125. #endif /* HSC */
  126.  
  127.